package edu.pku.sei.modeler.uml.commands.sequence;

import org.eclipse.draw2d.geometry.Rectangle;

import edu.pku.sei.modeler.model.core.MetaModelElement;
import edu.pku.sei.modeler.model.structure.MetaNode;
import edu.pku.sei.modeler.model.uml.BasicInteractions.Lifeline;
import edu.pku.sei.modeler.uml.commands.NestedElementCreateCommand;

public class UMLStateInvariantCommand extends NestedElementCreateCommand {
	private static final int STATEINVARIANT_WIDTH = 40;

	public UMLStateInvariantCommand(MetaNode node, MetaNode hostNode,
			Rectangle constraint) {
		super(node, hostNode, constraint);
	}

	@Override
	public boolean canExecute() {
		return super.canExecute()
				&& fatherNode.getModelElement() instanceof Lifeline;
	}

	@Override
	protected void undoModelElement() {
		Lifeline lifeline = (Lifeline) fatherNode.getModelElement();
		lifeline.getIncludedStateInvariants().remove(
				this.node.getModelElement());
	}

	@Override
	protected void updateModelElement() {
		MetaModelElement modelElement = this.node.getModelElement();
		Lifeline lifeline = (Lifeline) fatherNode.getModelElement();
		lifeline.getIncludedStateInvariants().add(modelElement);
	}

	@Override
	protected void updateNodeBounds() {
		int hostX = 0;
		int hostWidth = fatherNode.getWidth();

		hostX += hostWidth / 2;

		// regular operations
		node.setLocationX(hostX - STATEINVARIANT_WIDTH / 2);
		node.setLocationY(constraint.getLocation().y);
		node.setWidth(STATEINVARIANT_WIDTH);
		node.setHeight(constraint.height);
	}

}
